// pages/burgeon/burgeon.js
const app = getApp()
const { jumpShow } = require('../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isClick:false,
        isShow: false,
        stages: [
            { id: 'burgeon1', title: '0~2岁宝贝乐园', value: '启蒙1', stage: 1 },
            { id: 'burgeon2', title: '3~4岁宝贝乐园', value: '启蒙2', stage: 2 },
            { id: 'burgeon3', title: '5~6岁宝贝乐园', value: '启蒙3', stage: 3 }
        ],
        studyModules: [
            { name: 'science', title: '科普小驿站' },
            { name: 'safety', title: '安全小卫士' }
        ]
    },

    computeScrollViewHeight() {
        let query = wx.createSelectorQuery().in(this)
        query.select('.title-bar').boundingClientRect()
        query.exec((res) => {
          let titleBarHeight = res[0].height;
          let screenHeight = wx.getSystemInfoSync().windowHeight
            let scrollHeight = screenHeight - titleBarHeight
            this.setData({
              scrollListHeight: scrollHeight
            })
        })
      },



    //获取对应启蒙阶段趣味互动视频
    getinteractiveVideo: function () {
        let stage = this.data.selectedStage.stage;
        wx.request({
            url: `${app.globalData.url}/video/interactive_Video?stage=${stage}`,
            success: res => {
                // console.log(res.data);
                this.setData({
                    videoList: res.data.slice(0, 6)
                })
                wx.setStorageSync('videoList', res.data)
            }
        })
    },

    //在首页展示认知图片
    showCognize: function () {
        wx.request({
            url: `${app.globalData.url}/cognizes/cognize`,
            success: res => {
                if (res.statusCode == 200) {
                    wx.setStorageSync('cognizeList', res.data)
                    this.setData({ cognizes: res.data.slice(0, 3) })
                }
            }
        })
    },

    //在首页展示文化百科图片
    showCulture: function () {
        wx.request({
            url: `${app.globalData.url}/culture/culture_Knowledge`,
            success: res => {
                if (res.statusCode == 200) {
                    wx.setStorageSync('cultureList', res.data)
                    this.setData({ cultureList: res.data.slice(0, 2) })
                }
            }
        })
    },

    //在首页展示测试图片
    showTest: function () {
        wx.request({
            url: `${app.globalData.url}/funny/funny_Test`,
            success: res => {
                if (res.statusCode == 200) {
                    wx.setStorageSync('testList', res.data)
                    this.setData({ testList: res.data.slice(0, 3) })
                }
            }
        })
    },

    //获取科普小驿站或安全小卫士视频
    showSciOrSafeVideo: function (studyModule) {
        wx.request({
            url: `${app.globalData.url}/cognizes/${studyModule}`,
            success: res => {
                console.log(res.data);
                if (res.statusCode == 200) {
                    if (studyModule == 'science') {
                        this.setData({
                            sciencevideoList: res.data.slice(0, 4)
                        })
                        wx.setStorageSync('sciencevideoList', res.data)
                    } else {
                        this.setData({
                            safetyvideoList: res.data.slice(0, 4)
                        })
                        wx.setStorageSync('safetyvideoList', res.data)
                    }
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let age = wx.getStorageSync('user').age;
        // console.log(age)
        let stage = 1;
        if (age >= 0 && age <= 2) {
            stage = 1;
        } else if (age > 2 && age <= 4) {
            stage = 2
        } else {
            stage = 3
        }
        this.setData({ selectedStage: this.data.stages[stage - 1] })
        this.getinteractiveVideo();
        // console.log(this.data.selectedStage.stage)
        if (this.data.selectedStage.stage == 1) {
            this.showCognize()
        } else if (this.data.selectedStage.stage == 2) {
            this.showCulture()
            this.showTest()
        } else {
            this.showSciOrSafeVideo('science');
            this.showSciOrSafeVideo('safety');
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.computeScrollViewHeight()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    selectStage: function () {
        if(this.data.isShow){
            this.setData({ isShow: false });
        }else{
            this.setData({ isShow: true });
        }

        if(this.data.isClick){
            this.setData({isClick:false});
        }else{
            this.setData({isClick:true});
        }
    },

    skipStage: function (e) {
        this.setData({isClick:false});
        for (var i = 0; i < this.data.stages.length; i++) {
            if (this.data.stages[i].id == e.target.id) {
                this.setData({ selectedStage: this.data.stages[i] });
                this.getinteractiveVideo();
                if (this.data.selectedStage.stage == 1) {
                    this.showCognize();
                } else if (this.data.selectedStage.stage == 2) {
                    this.showCulture();
                    this.showTest();
                } else {
                    this.showSciOrSafeVideo('science');
                    this.showSciOrSafeVideo('safety');
                }
            }
        }
        setTimeout(() => {
            this.setData({ isShow: false });
        }, 200)
    },

    interactPlay: function (e) {
        jumpShow(e, 'interact');
    },

    sciencevideoPlay: function (e) {
        jumpShow(e, 'studyVideo', { studyModule: 'science' });
    },

    safetyvideoPlay: function (e) {
        jumpShow(e, 'studyVideo', { studyModule: 'safety' });
    },

    startCognize: function (e) {
        wx.request({
            url: `${app.globalData.url}/cognizes/cognize?cognize_Classify=${e.target.id}`,
            success: res => {
                if (res.statusCode == 200) {
                    jumpShow(e, 'cognize', { amount: res.data[0].amount });
                }
            }
        })
    },

    showList: function (e) {
        jumpShow(e, 'cultureList', { culture_Title: e.target.id == 'festival' ? '中华传统节日' : '寓言小故事' })
    },

    startTest: function (e) {
        wx.request({
            url: `${app.globalData.url}/funny/funny_Test?test_Classify=${e.target.id}`,
            success: res => {
                if (res.statusCode == 200) {
                    jumpShow(e, 'test', { amount: res.data[0].amount });
                }
            }
        })
    }

})